package com.yejhxt.api;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.gson.Gson;
import com.yejhxt.inter.deviceMapper;
import com.yejhxt.inter.messageMapper;
import com.yejhxt.inter.roleMapper;
import com.yejhxt.inter.sosMapper;
import com.yejhxt.inter.userMapper;
import com.yejhxt.model.device;
import com.yejhxt.model.message;
import com.yejhxt.model.role;
import com.yejhxt.model.user;
import com.yejhxt.tools.ProjectTools;

@Controller
@RequestMapping(value = "api/device/", method = RequestMethod.POST)
public class DeviceManager {
	@Resource
	private userMapper userMapper;
	@Resource
	private roleMapper roleMapper;
	@Resource
	private deviceMapper deviceMapper;
	@Resource
	private messageMapper messageMapper;
	@Resource
	private sosMapper sosMapper;

	@RequestMapping(value = "insertdevice.do", method = RequestMethod.POST)
	public void insertdevice(@RequestParam(value = "access_token" , defaultValue = "") String sid,
			@RequestParam(value = "devicecode", defaultValue = "") String devicecode,
			@RequestParam(value = "devicename", defaultValue = "") String devicename,
			@RequestParam(value = "holder", defaultValue = "") String holder, HttpServletRequest request, Model model,
			HttpSession session, HttpServletResponse response) {
		int status = 403;
		user user = null;
		if (session != null) {
			user = (user) session.getAttribute("User");
			if (user == null)
			session = ProjectTools.getAppSession(sid);
			if(session!=null)
			user = (user) session.getAttribute("User");
		}
		if (user == null)
			status = 403;
		else if (StringUtils.isEmpty(devicename) || StringUtils.isEmpty(devicecode)
				||StringUtils.isEmpty(holder))
			status = 406;
		else {
			Map<String, Object> map1=new HashMap<>();
			map1.put("devicecode",devicecode);
			List<device> device =deviceMapper.selectByConditions(map1);
			if(device==null||device.isEmpty())
			{
			device d = new device();
			d.setDevicecode(devicecode);
			d.setDevicename(devicename);
			d.setHolder(holder);
			d.setCreatetime(new Date());
			d.setModifytime(new Date());
			d.setRoleid(user.getUserid());
			deviceMapper.insertSelective(d);
			status = 200;
			}
			else
				status=406;
		}
     
		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200) {
			map.put("status", "success");
			map.put("code", 200);
		} else if (status == 403) {
			map.put("status", "failure");
			map.put("code", 403);
			map.put("error_description", "Not Acceptable");
		} else {
        
			map.put("status", "failure");
			map.put("code", 406);
			map.put("error_description", "parameters error");

		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
	@RequestMapping(value = "getdevicelist.do")
	public void insertdevice(@RequestParam(value = "access_token" , defaultValue = "") String sid,HttpServletRequest request, Model model,
			HttpSession session, HttpServletResponse response) {
		int status = 403;
		role role=null;
		user user = null;
		if (session != null) {
			user = (user) session.getAttribute("User");
			if (user == null)
			session = ProjectTools.getAppSession(sid);
			if(session!=null)
			user = (user) session.getAttribute("User");
		}
		if (user == null)
			status = 403;
		else {

			role =roleMapper.selectByPrimaryKeyWithDevices(user.getUserid());
		
			status = 200;
		}
     
		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200) {
			map.put("status", "success");
			map.put("code", 200);
			map.put("result", role);
		} else if (status == 403) {
			map.put("status", "failure");
			map.put("code", 403);
			map.put("error_description", "Not Acceptable");
		} else {
        
			map.put("status", "failure");
			map.put("code", 406);
			map.put("error_description", "parameters error");

		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
	
	@RequestMapping(value = "delete.do", method = RequestMethod.POST)
	public void delete(@RequestParam(value = "access_token" , defaultValue = "") String sid,
			@RequestParam(value = "deviceid", defaultValue = "") int deviceid,
			 HttpServletRequest request, Model model,
			HttpSession session, HttpServletResponse response) {
		System.out.println(deviceid);
		int status = 403;
		user user = null;
		if (session != null) {
			user = (user) session.getAttribute("User");
			if (user == null)
				session = ProjectTools.getAppSession(sid);
			if(session!=null)
			user = (user) session.getAttribute("User");
		}
		if (user == null)
			status = 403;
		else if (deviceid<=0)
			status = 406;
		else {
			
			sosMapper.deleteByDeviceId(new Long(deviceid));
			messageMapper.deleteByDeviceId(new Long(deviceid));
			deviceMapper.deleteByPrimaryKey(new Long(deviceid));
		
			status = 200;
		}
     
		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200) {
			map.put("status", "success");
			map.put("code", 200);
		} else if (status == 403) {
			map.put("status", "failure");
			map.put("code", 403);
			map.put("error_description", "Not Acceptable");
		} else {
        
			map.put("status", "failure");
			map.put("code", 406);
			map.put("error_description", "parameters error");

		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
	@RequestMapping(value = "/batch/delete.do")
	public void batchdelete(@RequestParam(value = "access_token" , defaultValue = "") String sid,
			@RequestParam(value = "deviceid", defaultValue = "") String deviceid,
			 HttpServletRequest request, Model model,
			HttpSession session, HttpServletResponse response) {
		int status = 403;
		user user = null;
		if (session != null) {
			user = (user) session.getAttribute("User");
			if (user == null)
				session = ProjectTools.getAppSession(sid);
			if(session!=null)
			user = (user) session.getAttribute("User");
		}
		if (user == null)
			status = 403;
		else if (StringUtils.isEmpty(deviceid))
			status = 406;
		else {
			String [] ids=deviceid.split(",");
		
			for(String i:ids){
				
				messageMapper.deleteByDeviceId(new Long(i));
				deviceMapper.deleteByPrimaryKey(new Long(i));
				
			}
			
		
		
			status = 200;
		}
     
		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200) {
			map.put("status", "success");
			map.put("code", 200);
		} else if (status == 403) {
			map.put("status", "failure");
			map.put("code", 403);
			map.put("error_description", "Not Acceptable");
		} else {
        
			map.put("status", "failure");
			map.put("code", 406);
			map.put("error_description", "parameters error");

		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
	@RequestMapping(value = "/getoneinfo.do")
	public void getoneinfo(@RequestParam(value = "access_token" , defaultValue = "") String sid,
			@RequestParam(value = "devicecode", defaultValue = "") String devicecode,
			@RequestParam(value = "dateBegin", defaultValue = "") String dateBegin,
			@RequestParam(value = "dateEnd", defaultValue = "") String dateEnd,
			 HttpServletRequest request, Model model,
			HttpSession session, HttpServletResponse response) throws ParseException {
		 device m = null;
		 SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd hh:mm");
		int status = 403;
		user user = null;
		if (session != null) {
			user = (user) session.getAttribute("User");
			if (user == null)
				session = ProjectTools.getAppSession(sid);
			if(session!=null)
			user = (user) session.getAttribute("User");
		}
		if (user == null)
			status = 403;
		else if (StringUtils.isEmpty(devicecode)&&StringUtils.isEmpty(dateBegin)&&StringUtils.isEmpty(dateEnd))
			status = 406;
		else {
              
			Map<String, Object> map1=new HashMap<>();
			map1.put("devicecode",devicecode);
			map1.put("roleid",user.getUserid());
			List<device> device =deviceMapper.selectByConditions(map1);
			if(device!=null||!device.isEmpty())
			{
	
	      map1.clear();		
			map1.put("begin", s.parse(dateBegin));
			map1.put("end", s.parse(dateEnd));
			map1.put("deviceid", device.get(0).getDeviceid());
		 m=deviceMapper.selectByPrimaryKeyWithMessages(map1);
		 status = 200;
			
			}
			else
				status=500;
		}
     
		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200) {
			map.put("status", "success");
			map.put("code", 200);
			map.put("result", m);
		} else if (status == 403) {
			map.put("status", "failure");
			map.put("code", 403);
			map.put("error_description", "Not Acceptable");
		} else if(status==500){
			map.put("status", "failure");
			map.put("code", 500);
			map.put("error_description", "没有这个设备");
			
		}
		else{
        
			map.put("status", "failure");
			map.put("code", 406);
			map.put("error_description", "parameters error");

		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
	@RequestMapping(value = "/deleteoneinfo.do")
	public void deleteoneinfo(@RequestParam(value = "access_token" , defaultValue = "") String sid,
			@RequestParam(value = "messageid", defaultValue = "") String messageid,HttpServletRequest request, Model model,
			HttpSession session, HttpServletResponse response) throws ParseException {
		int status = 403;
		user user = null;
		if (session != null) {
			user = (user) session.getAttribute("User");
			if (user == null)
				session = ProjectTools.getAppSession(sid);
			if(session!=null)
			user = (user) session.getAttribute("User");
		}
		if (user == null)
			status = 403;
		else if (StringUtils.isEmpty(messageid))
			status = 406;
		else {
           messageMapper.deleteByPrimaryKey(new Long(messageid));
		 status = 200;
		}
     
		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200) {
			map.put("status", "success");
			map.put("code", 200);
		} else if (status == 403) {
			map.put("status", "failure");
			map.put("code", 403);
			map.put("error_description", "Not Acceptable");
		} else {
        
			map.put("status", "failure");
			map.put("code", 406);
			map.put("error_description", "parameters error");

		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
	@RequestMapping(value = "/batch/deletemessages.do")
	public void batchdeletemessage(@RequestParam(value = "access_token" , defaultValue = "") String sid,
			@RequestParam(value = "messageid", defaultValue = "") String messageid,
			 HttpServletRequest request, Model model,
			HttpSession session, HttpServletResponse response) {
		int status = 403;
		user user = null;
		if (session != null) {
			user = (user) session.getAttribute("User");
			if (user == null)
				session = ProjectTools.getAppSession(sid);
			if(session!=null)
			user = (user) session.getAttribute("User");
		}
		if (user == null)
			status = 403;
		else if (StringUtils.isEmpty(messageid))
			status = 406;
		else {
			String [] ids=messageid.split(",");
		
			for(String i:ids){
				
				messageMapper.deleteByPrimaryKey(new Long(messageid));
				
			}
			
		
		
			status = 200;
		}
     
		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200) {
			map.put("status", "success");
			map.put("code", 200);
		} else if (status == 403) {
			map.put("status", "failure");
			map.put("code", 403);
			map.put("error_description", "Not Acceptable");
		} else {
        
			map.put("status", "failure");
			map.put("code", 406);
			map.put("error_description", "parameters error");

		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
	@RequestMapping(value = "/deletebytime.do")
	public void deletebytime(@RequestParam(value = "access_token" , defaultValue = "") String sid,
			@RequestParam(value = "deviceid", defaultValue = "") String deviceid,
			@RequestParam(value = "dateBegin", defaultValue = "") String dateBegin,
			@RequestParam(value = "dateEnd", defaultValue = "") String dateEnd,
			 HttpServletRequest request, Model model,
			HttpSession session, HttpServletResponse response) throws ParseException {
		 SimpleDateFormat s=new SimpleDateFormat("yyyy-MM-dd hh:mm");
		int status = 403;
		user user = null;
		if (session != null) {
			user = (user) session.getAttribute("User");
			if (user == null)
			session = ProjectTools.getAppSession(sid);
			if(session!=null)
			user = (user) session.getAttribute("User");
		}
		if (user == null)
			status = 403;
		else if (StringUtils.isEmpty(deviceid)&&StringUtils.isEmpty(dateBegin)&&StringUtils.isEmpty(dateEnd))
			status = 406;
		else {
	
			Map<String, Object> map1=new HashMap<>();
			map1.put("begin", s.parse(dateBegin));
			map1.put("end", s.parse(dateEnd));
			map1.put("deviceid", deviceid);
		 messageMapper.deleteByTimes(map1);
		 status = 200;
		}
     
		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200) {
			map.put("status", "success");
			map.put("code", 200);
		} else if (status == 403) {
			map.put("status", "failure");
			map.put("code", 403);
			map.put("error_description", "Not Acceptable");
		} else {
        
			map.put("status", "failure");
			map.put("code", 406);
			map.put("error_description", "parameters error");

		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
	
	
	@RequestMapping(value = "/getlastmessage.do")
	public void getlastmessage(@RequestParam(value = "access_token" , defaultValue = "") String sid,
			 HttpServletRequest request, Model model,
			HttpSession session, HttpServletResponse response) throws ParseException {
		int status = 403;
		user user = null;
		List<device> m=new ArrayList<device>();
		if (session != null) {
			user = (user) session.getAttribute("User");
			if (user == null)
				session = ProjectTools.getAppSession(sid);
			if(session!=null)
			user = (user) session.getAttribute("User");
		}
		if (user == null)
			status = 403;
		else {
              
			Map<String, Object> map1=new HashMap<>();
			map1.put("roleid",user.getUserid());
			List<device> device =deviceMapper.selectByConditions(map1);
			if(device!=null)
			{
				
				if(!device.isEmpty())
				{
				for(int i=0;i<device.size();i++)
				{
					 map1.clear();	
		    	map1.put("deviceid", device.get(i).getDeviceid());
		    	Map map=new HashMap();
		    	map.put("deviceid", device.get(i).getDeviceid());
		    	device.get(i).setMessage( messageMapper.GetLatestOneOfDev(map));
		    	
		       m.add(device.get(i));
				}
				}
				if(m.isEmpty())
					m=null;
					
		 status = 200;
			
			}
			else
				status=500;
		}
     
		Map<String, Object> map = ProjectTools.returnMap(status);
		if (status == 200) {
			map.put("status", "success");
			map.put("code", 200);
			map.put("result", m);
		} else if (status == 403) {
			map.put("status", "failure");
			map.put("code", 403);
			map.put("error_description", "Not Acceptable");
		} else if(status==500){
			map.put("status", "failure");
			map.put("code", 500);
			map.put("error_description", "没有这个设备");
			
		}
		else{
        
			map.put("status", "failure");
			map.put("code", 406);
			map.put("error_description", "parameters error");

		}
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}
}

